import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class TestParentData extends StatefulWidget {
  const TestParentData({super.key});

  @override
  State<TestParentData> createState() => _TestParentDataState();
}

class _TestParentDataState extends State<TestParentData> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("test parent Data"),
      ),
      body: Column(
        children: [
          Container(
            width: 100,
            height: 100,
            child: Stack(
              children: [
                CustomParentDataWidget(
                  child: Text("1231111"),
                  left: 40.0,
                ),
                Positioned(
                  child: Container(
                    width: 20,
                    height: 20,
                    color: Colors.black,
                  ),
                  left: 10,
                  top: 10,
                )
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class CustomParentDataWidget extends ParentDataWidget<StackParentData> {
  const CustomParentDataWidget({
    super.key,
    required super.child,
    this.left,
  });

  final double? left;

  @override
  void applyParentData(RenderObject renderObject) {
    assert(renderObject.parentData is StackParentData);
    final StackParentData parentData =
        renderObject.parentData! as StackParentData;
    bool needsLayout = false;

    if (parentData.left != left) {
      parentData.left = left;
      needsLayout = true;
    }
    if (needsLayout) {
      final RenderObject? targetParent = renderObject.parent;
      if (targetParent is RenderObject) {
        targetParent.markNeedsLayout();
      }
    }
  }

  @override
  Type get debugTypicalAncestorWidgetClass => Stack;
}
